/*
 * @Des: 
 * @Author: jin_yc
 * @Date: 2020-01-06 15:46:12
 * @LastEditor: jin_yc
 * @LastEditTime: 2022-04-11 14:50:34
 */
import './config/microapp/public-path'
import Vue from 'vue'
import { LOADING_INIT_DATA, LOADING_USER_DATA } from '@/store/Action/actionTypes'
import { CLEAN_WEBSOCKET_POOL } from '@/store/Mutation/mutationTypes'
import '@/assets/css/app.scss' //app公共样式
import { tsDvalue } from './config'
import Api from '@/config/api'
import { initAsSubApp, initAsIndependentApp } from '@/microapp/util'

Vue.config.productionTip = false

//在install前注入环境变量
Object.assign(Vue.prototype, Api)


//确保独立运行,并设置独立运行时的参数
if (!window.__POWERED_BY_QIANKUN__) {
  initAsIndependentApp()
  render()
}

var instance = null

/**
 * @description: 初始化入口
 * 
 */
async function render (props = {}) {
  const { container } = props

  //toDo 为了保证环境变量注入完成，再加载路由
  const { setTs } = require('@/utils/request/sign')
  const store = require('@/store').default
  const install = require('@/utils/hooks/loadPlugins').default
  const locale = require('@/utils/locale/index').default
  const router = require('@/permission').default
  const App = require('@/App.vue').default

  Vue.use(install)
  await setTs()
  window.setInterval(() => {
    setTs()
  }, tsDvalue)
  await store.dispatch(LOADING_INIT_DATA)
  await store.dispatch(LOADING_USER_DATA)
  Vue.use(locale)
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#fks-app') : '#fks-app')

}


/**
 * @description: 第一次进入子应用后调用
 *               将父应用传递的值挂载vue原型上
 */
export async function bootstrap (props = {}) {
  initAsSubApp(props)
}


/**
 * @description: 每次进入入子应用后调用
 *               第一次进入时会在boostrap方法完成后调用
 */
export async function mount (props = {}) {
  await render(props)
}


/**
 * @description: 每次切出子应用后调用
 *               断开子应用的websocket连接并销毁Vue实例
 */
export async function unmount () {
  const store = require('@/store').default
  store.commit(CLEAN_WEBSOCKET_POOL)
  if (instance) {
    instance.$el.innerHTML = ''
    instance.$destroy()
    instance = null
  }
}


